package com.ynet.middleground.mobile.approve.controller;

import javax.validation.Valid;

import com.ynet.middleground.mobile.approve.request.QueryAllChannelFormatReq;
import com.ynet.middleground.mobile.approve.response.ChannelFormatDTO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Throwables;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.dao.MobileVersionMapper;
import com.ynet.middleground.mobile.approve.entity.MobileVersion;
import com.ynet.middleground.mobile.approve.request.GetUserInfoReq;
import com.ynet.middleground.mobile.approve.response.MobileVersionDTO;
import com.ynet.middleground.mobile.approve.response.UserInfoDTO;
import com.ynet.middleground.mobile.approve.service.UserCenterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 项目名：ifp-mobile-approve <br>
 * 包名：com.ynet.middleground.mobile.approve.controller <br>
 * 类名：MyController <br>
 * 描述：
 *
 * @author Suremotoo
 * @create 2021-03-30 19:49
 */
@RestController
@Api(value = "我的信息服务", tags = "我的信息服务")
public class MyController {

    @Autowired
    UserCenterService userCenterService;

    @Autowired
    MobileVersionMapper mobileVersionMapper;

    @Autowired
    Mapper mapper;

    /**
     * 获取用户信息
     * 
     * @param req
     * @return
     */
    @PostMapping("/profile")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public ResponseResult<UserInfoDTO> profile(@RequestBody @Valid GetUserInfoReq req) {
        ResponseResult<UserInfoDTO> result = new ResponseResult<>();
        try {
            UserInfoDTO dto = userCenterService.getUserInfoByDingTalkUserId(req.getUserId(), req.getDingtalkUserId());
            result.setData(dto);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取我的信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
        }
        return result;
    }

    /**
     * 查询移动审批版本服务
     * 
     * @param req 当前用户信息，防止非钉钉用户请求
     * @return
     */
    @PostMapping("/version")
    @ApiOperation(value = "查询移动审批版本服务", notes = "查询移动审批版本服务")
    public ResponseResult<MobileVersionDTO> version() {
        ResponseResult<MobileVersionDTO> result = new ResponseResult<>();
        try {
            LambdaQueryWrapper<MobileVersion> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(MobileVersion::getGmtCreate);
            queryWrapper.last("limit 1");
            MobileVersion mobileVersion = mobileVersionMapper.selectOne(queryWrapper);
            result.setData(mobileVersion == null ? null : mapper.map(mobileVersion, MobileVersionDTO.class));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询移动审批版本服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
        }
        return result;
    }

    /**
     * 查询所有渠道业态服务
     *
     * @param
     * @return
     */
    @PostMapping("/channel-format")
    @ApiOperation(value = "查询渠道列表（附业态公司）", notes = "查询渠道列表（附业态公司）")
    public ResponseResult<List<ChannelFormatDTO>> queryAllChannelFormat(@RequestBody @Valid QueryAllChannelFormatReq req) {
        ResponseResult<List<ChannelFormatDTO>> result = new ResponseResult<>();
        try {
            List<ChannelFormatDTO> dtos = userCenterService.getChannelFormatList(req.getUserId());
            result.setData(dtos);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询所有渠道和业态, case: {}",
                    Throwables.getStackTraceAsString(e));
        }
        return result;
    }
}
